/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DiscoveryDialog.java
 *
 * Created on Dec 10, 2010, 6:24:20 PM
 */
package com.bff.slimserver.musicjukebox.ui.settings;

import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import java.awt.Dimension;
import java.net.InetAddress;

/**
 *
 * @author Bill
 */
public class DiscoveryDialog extends javax.swing.JDialog {

    private InetAddress selectedServer;

    /** Creates new form DiscoveryDialog */
    public DiscoveryDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        discoveryPanel = new DiscoveryPanel();
        jPanel2 = new BJJPanel();
        btnSelect = new BJJButton();
        btnCancel = new BJJButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SqueezeBox Server Discovery");

        discoveryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("SqueezeBox Discovery"));
        getContentPane().add(discoveryPanel, java.awt.BorderLayout.CENTER);

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });
        jPanel2.add(btnSelect);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        this.selectedServer = getDiscoveryPanel().getSelectedServer();
        dispose();
    }//GEN-LAST:event_btnSelectActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.selectedServer = null;
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DiscoveryDialog dialog = new DiscoveryDialog(new javax.swing.JFrame(), true);
                dialog.setSize(new Dimension(600, 200));
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSelect;
    private javax.swing.JPanel discoveryPanel;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    public void startSearch() {
        getDiscoveryPanel().startSearch();
    }

    public void stopSearch() {
        getDiscoveryPanel().stopSearch();
    }

    @Override
    public void dispose() {
        super.dispose();
        stopSearch();
    }


    /**
     * @return the selectedServer
     */
    public InetAddress getSelectedServer() {
        return selectedServer;
    }

    public DiscoveryPanel getDiscoveryPanel() {
        return (DiscoveryPanel) discoveryPanel;
    }
}
